package cn.ivicar.sim.util;

import java.math.BigDecimal;
import java.text.ParseException;

import org.apache.commons.lang.StringUtils;

import cn.com.carsmart.ws.model.SimFlowInfo;
import cn.com.carsmart.ws.model.SimFlowPackageInfo;
import cn.com.carsmart.ws.model.SimInfo;
import net.sf.json.JSONObject;

/**
 * Created by lixiaozhuo on 17/5/21.
 */
public class ConvertUtils {

    public static final String SUCCESS_CODE = "0000";
    public static final String KEY_RETURN_CODE = "code";

    public static SimFlowPackageInfo convertFLowPackage(JSONObject json){
        //TODO 数据库中缺少运营商字段
        //TODO 确认套餐的价钱是哪个字段，并添加到数据库中
        //TODO 确认流量的单位是什么
        if(json == null){
            return null;
        }

        SimFlowPackageInfo flowPackageInfo = new SimFlowPackageInfo();
        flowPackageInfo.setOperator(json.optInt("operator"));
        flowPackageInfo.setType(json.optInt("priority"));
        flowPackageInfo.setName(json.optString("title"));
        flowPackageInfo.setFlowPackageId(json.optString("tcsyid"));
        flowPackageInfo.setActiveMonth(json.optInt("flow_month"));
        flowPackageInfo.setFlowTotal(json.optInt("flow_total"));
        flowPackageInfo.setPrice(new BigDecimal(json.optString("price1")));
        flowPackageInfo.setRefPackageId(json.optString("tcsyid"));
        String strModified = json.optString("modifyDate");
        if(StringUtils.isNotBlank(strModified)) {
            try {
                flowPackageInfo.setModified(DateUtil.TIMEFORMATER1().parse(strModified));
            } catch (ParseException e) {
                System.out.println("流量套餐更新时间解析失败 time=" + strModified);
            }
        }

        return flowPackageInfo;
    }

//    public static SimInfo convertSimInfo(JSONObject json){
//        if(json == null){
//            return null;
//        }
//
//        SimInfo simInfo = new SimInfo();
//        simInfo.setIccid(json.optString("iccid"));
//        simInfo.setOperator(json.optInt("operator"));
//        simInfo.setCarStatus(json.optInt("carStatus"));
//        simInfo.setUserCode(json.optString("code"));
//        String strEffectTime = json.optString("effectDate");
//        if(StringUtils.isNotBlank(strEffectTime)) {
//            try {
//                simInfo.setEffectTime(DateUtil.TIMEFORMATER1().parse(strEffectTime));
//            } catch (ParseException e) {
//                System.out.println("激活时间解析失败 time=" + strEffectTime);
//            }
//        }
//        simInfo.setSmsStatus(json.optInt("smsflag"));
//        simInfo.setOrgId(1000);
//
//        return simInfo;
//    }

    public static SimInfo convertSimInfo(JSONObject simBaseJson, JSONObject simStatusJson, JSONObject gprsStatusJson, JSONObject switchStatusJson) {
        if (simBaseJson == null || !SUCCESS_CODE.equals(simBaseJson.opt(KEY_RETURN_CODE))) {
            return null;
        }

        JSONObject json = simBaseJson.getJSONArray("card_info").getJSONObject(0);
        SimInfo simInfo = new SimInfo();
        simInfo.setIccid(json.optString("iccid"));

        if (simStatusJson != null && SUCCESS_CODE.equals(simStatusJson.opt(KEY_RETURN_CODE))) {
            simInfo.setCarStatus(simStatusJson.getJSONObject("result").optString("status"));
        }

        if (gprsStatusJson != null && SUCCESS_CODE.equals(gprsStatusJson.opt(KEY_RETURN_CODE))) {
            JSONObject result = gprsStatusJson.getJSONObject("result");
            simInfo.setGprsStatus(result.optString("gprs_status"));
            simInfo.setIsPauseGprs(result.optString("is_pause_gprs"));
        }

        if (switchStatusJson != null && SUCCESS_CODE.equals(switchStatusJson.opt(KEY_RETURN_CODE))) {
            simInfo.setSwitchStatus(switchStatusJson.getJSONObject("result").optString("switch_status"));
        }

        simInfo.setOrgId(1000);
        return simInfo;
    }

    public static SimFlowInfo convertSimFlowInfo(JSONObject json){
        if(json == null || !SUCCESS_CODE.equals(json.opt(KEY_RETURN_CODE))){
            return null;
        }

        JSONObject result = json.getJSONObject("result");
        int total = result.optInt("flow_package");
        int used = result.optInt("flow_spend");
        int remain = total - used;

        SimFlowInfo simFlowInfo = new SimFlowInfo();
        simFlowInfo.setFlowAmount(total);
        simFlowInfo.setFlowUsed(used);
        simFlowInfo.setFlosRemaim(remain);

        return simFlowInfo;
    }
}
